package com.shinvent.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.shinvent.common.Logger;
import com.shinvent.dao.BaseDao;
import com.shinvent.dao.V_ITEMCOUNTDao;
import com.shinvent.dao.V_STATISTICDao;

public class Statistic extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public Statistic() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml");
	    PrintWriter out = response.getWriter();
	    
	    try{
	    	String statType = request.getParameter("stat");
	    	if(statType.equals("yearly")){
	    		String year = request.getParameter("year");
	    		out = this.getYearly(out,year);
	    	}else if(statType.equals("monthly")){
	    		String year = request.getParameter("year");
	    		String month = request.getParameter("month");
	    		out = this.getMonthly(out,year, month);
	    	}
	    }catch(Exception e){
	    	e.printStackTrace();
	    	Logger.logError(e);
	    }
	    
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	private PrintWriter getYearly(PrintWriter out, String year) throws Exception{
		V_STATISTICDao statDao = null;
		
		try{
			statDao = new V_STATISTICDao();
			statDao.query("YEAR_ID = " + year + " GROUP BY MONTH_ID ORDER BY MONTH_ID ASC");
			
			out.println("<?xml version=\"1.0\"?>");
	    	out.println("<STATS>");
	    	
			while(statDao.gotoNext()){
				out.println("<STAT>");
					out.println("<YEAR_ID>");
						out.println(statDao.getProperty("YEAR_ID")!=null ? statDao.getString("YEAR_ID") : "0");
					out.println("</YEAR_ID>");
					out.println("<YEAR_VALUE>");
						out.println(statDao.getProperty("YEAR_VALUE")!=null ? statDao.getString("YEAR_VALUE") : "NOT STATED");
					out.println("</YEAR_VALUE>");
					out.println("<MONTH_ID>");
						out.println(statDao.getProperty("MONTH_ID")!=null ? statDao.getString("MONTH_ID") : "0");
					out.println("</MONTH_ID>");
					out.println("<MONTH_NAME>");
						out.println(statDao.getProperty("MONTH_NAME")!=null ? statDao.getString("MONTH_NAME") : "NOT STATED");
					out.println("</MONTH_NAME>");
					out.println("<PRICE>");
						out.println(statDao.getString("PRICE"));
					out.println("</PRICE>");
//					out.println("<ITEM_COUNT>");
//						out.println(statDao.getString("ITEM_COUNT"));
//					out.println("</ITEM_COUNT>");
//					out.println("<VENDOR_ID>");
//						out.println(statDao.getProperty("VENDOR_ID")!=null ? statDao.getString("VENDOR_ID") : "0");
//					out.println("</VENDOR_ID>");
//					out.println("<VENDOR_NAME>");
//						out.println(statDao.getProperty("VENDOR_NAME")!=null ? statDao.getString("VENDOR_NAME") : "NOT STATED");
//					out.println("</VENDOR_NAME>");
					out.println("</STAT>");
			}
			
			out.println("</STATS>");
			release(statDao);
			return out;
		}catch(Exception e){
			e.printStackTrace();
			Logger.logError(e);
			throw new Exception();
		}finally{
			release(statDao);
		}
		
	}
	
	private PrintWriter getMonthly(PrintWriter out, String year, String month) throws Exception{
		V_ITEMCOUNTDao statDao = null;
		
		try{
			statDao = new V_ITEMCOUNTDao();
			statDao.query("YEAR_ID = " + year + " AND MONTH_ID = " + month);
			
			out.println("<?xml version=\"1.0\"?>");
	    	out.println("<STATS>");
	    	
			while(statDao.gotoNext()){
				out.println("<STAT>");
					out.println("<YEAR_ID>");
						out.println(statDao.getProperty("YEAR_ID")!=null ? statDao.getString("YEAR_ID") : "0");
					out.println("</YEAR_ID>");
					out.println("<MONTH_ID>");
						out.println(statDao.getProperty("MONTH_ID")!=null ? statDao.getString("MONTH_ID") : "0");
					out.println("</MONTH_ID>");
					out.println("<MONTH_NAME>");
						out.println(statDao.getProperty("MONTH_NAME")!=null ? statDao.getString("MONTH_NAME") : "NOT STATED");
					out.println("</MONTH_NAME>");
					out.println("<ITEM_COUNT>");
						out.println(statDao.getString("ITEM_COUNT"));
					out.println("</ITEM_COUNT>");
					out.println("<VENDOR_ID>");
						out.println(statDao.getString("VENDOR_ID")!=null ? statDao.getString("VENDOR_ID") : "0");
					out.println("</VENDOR_ID>");
					out.println("<VENDOR_NAME>");
						out.println(statDao.getString("VENDOR_NAME")!=null ? statDao.getString("VENDOR_NAME") : "NOT STATED");
					out.println("</VENDOR_NAME>");
					out.println("</STAT>");
			}
			
			out.println("</STATS>");
			release(statDao);
			return out;
		}catch(Exception e){
			e.printStackTrace();
			Logger.logError(e);
			throw new Exception();
		}finally{
			release(statDao);
		}
	}
	
	
	private void release(BaseDao dao){
		try{
			dao.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	

}
